function [crd, nrm, col] = vmvreadpos(fid, nrc, usecol)
% vmvreadpos  - read one vertex movie position in a VMV file
%
% FORMAT:       [crd, nrm, col] = vmvreadpos(fid, nrc, usecol)
%
% Input fields:
%
%       fid         1x1 file id (fopen, fread, fclose)
%       nrc         number of coordinates
%       usecol      use colors in VMV
%
% Output fields:
%
%       crd         Cx3 coordinates
%       nrm         Cx3 normals
%       col         Cx4 SRF-like color coding (or empty if unused)

% Version:  v0.7c
% Build:    7121414
% Date:     Dec-14 2007, 2:33 PM CST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% check arguments
if nargin ~= 3 || ...
   ~isa(fid, 'double') || ...
    numel(fid) ~= 1 || ...
    isinf(fid) || ...
    isnan(fid) || ...
    fid < 1 || ...
    fid ~= fix(fid) || ...
   ~isa(nrc, 'double') || ...
    numel(nrc) ~= 1 || ...
    isinf(nrc) || ...
    isnan(nrc) || ...
    nrc < 1 || ...
    nrc ~= fix(nrc) || ...
    numel(usecol) ~= 1
    error( ...
        'BVQXtools:BadArgument', ...
        'This call requires exactly 3 arguments.' ...
    );
end
try
    fpos = 0;
    if usecol
        rwwidth = 7;
    else
        rwwidth = 6;
    end
    fpos = ftell(fid);
    tdat = fread(fid, [rwwidth, nrc], '*single')';
    crd = tdat(:, 1:3);
    nrm = tdat(:, 4:6);
    if usecol
        fseek(fid, fpos, -1);
        tdat = fread(fid, [rwwidth, nrc], '*uint32')';
        col = uint322colcode(tdat(:, 7));
    else
        col = [];
    end
catch
    error( ...
        'BVQXtools:InternalError', ...
        'Error reading movie position in file id %d at position %d.', ...
        fid, fpos ...
    );
end
